<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/3
 * Time: 10:40
 */
namespace app\online\controller;

use think\Db;
use think\Request;

class Mail extends Common
{
	//判断是否登录
	public function _initialize()
	{
		parent::_initialize();
	}
	/**
	 *	邮寄地址列表
	 * @param string get.title 需要检索的商品名称
	*/
	public function index()
	{
		$where = ['query'=>[]];
		$db = Db::table('business')->where('status',2);
		if(!empty(trim(input('get.title')))){
			$title = trim(input('get.title'));
			$db->where('linkman|telephone','like',"%{$title}%");
			$where['query']['title'] = $title;
		}
		$result = $db->order('id desc')->paginate(8,false,$where);
		return view('index',compact('result'));
	}
	/**
	 * 邮寄列表
	 * @param string get.bid 商家id
	*/
	public function order()
	{
		$where = ['query'=>[]];
		$bid = input('get.bid');
		$where['query']['bid'] = $bid;
		$db = Db::table('mail')->where('status','<>',0)->order('id desc')->where('bid',$bid);
		if(!empty(trim(input('get.title')))){
			$title = trim(input('get.title'));
			$db->where('linkman|telephone|ordernumber','like',"%{$title}%");
			$where['query']['title'] = $title;
		}
		$result = $db->group('ordernumber')->paginate(8,false,$where);
		return view('order',compact('result','title','bid'));
	}
	/**
	 * 查看图片
	 * @param string get.id 邮寄表id
	*/
	public function enlarge()
	{
		$image = Db::table('mail')->where('id',input('get.id'))->value('prove');
		return view('enlarge',compact('image'));
	}
	/**
	 * 订单详情
	 * @param string get.ordernumber 订单号
	*/
	public function detail()
	{
		$result = Db::table('mail')
				->where('ordernumber',trim(input('ordernumber')))->paginate(10);
		return view('detail',compact('result'));
	}
	/**
	 * 发货
	 * @param string get.id 邮寄表id
	 * @param string get.express 快递公司名称
	 */
	public function delivery()
	{
		if(request()->isAjax()){
			try{
				$id = input('post.id');
				$express = input('post.express');
				$bool = Db::table('mail')->where('id',$id)->update(['status'=>2,'express'=>$express]);
				if(!$bool){
					throw new \PDOException('操作失败');
				}
				$success = ['success'=>'操作成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	/**
	 * 编辑订单
	 * @param string get.id 邮寄表id
	*/
	public function edit()
	{
		$result = Db::table('mail')->where('id',trim(input('get.id')))->find();
		return view('edit',compact('result'));
	}
	/**
	 * 更新订单
	 * @param string post.id 邮寄表id
	 * @param string post.linkman 邮寄人
	 * @param string post.telephone 联系电话
	 * @param string post.address 邮寄具体地址
	 * @param string post.province[0] 省
	 * @param string post.province[1] 市
	 * @param string post.province[2] 县
	 * @param string post.province[3] 镇(乡)
	 * @param string post.address 邮寄地址
	*/
	public function update(Request $request)
	{
		if(request()->isAjax()){
			try{
				$info = $request->param();
				$input['linkman'] = $info['linkman'];
				$input['telephone'] = $info['telephone'];
				$input['address'] = $info['address'];
				//如果修改邮寄地址
				if(count($info['province'])>1){
					$district = Db::table('district')->whereIn('id',$info['province'])->column('name');
					//省
					$input['province'] = $district[0];
					//市
					$input['city'] = $district[1];
					//县
					$input['county'] = $district[2];
					if(count($district) == 4){
						//镇
						$input['town'] = $district[3];
					}
				}
				$bool = Db::table('mail')->where('id',trim(input('post.id')))->update($input);
				if(!$bool){
					throw new \PDOException('修改失败');
				}
				$success = ['success'=>'修改成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	/**
	 * 已到
	 * @param string post.id 邮寄表id
	*/
	public function reach()
	{
		if(request()->isAjax()){
			try{
				$id = input('post.id');
				$result = Db::table('mail')->where('id',$id)->update(['status'=>3]);
				if(!$result){
					throw new \PDOException('操作失败');
				}
				$success = ['success'=>'操作成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	
	public function aaa()
	{
		test();
	}
}